package main

import (
	"context"
	"github.com/gorilla/websocket"
	"net/http"
)

type WsServerManager struct {
	upgrader websocket.Upgrader
	conn     *websocket.Conn
	err      error
}

func (wss *WsServerManager) Close() {
}

func (wss *WsServerManager) Loop(ctx context.Context) {
	return
}

func (wss *WsServerManager) Handle(w http.ResponseWriter, r *http.Request) {
	wss.conn, wss.err = wss.upgrader.Upgrade(w, r, nil)
	if wss.err != nil {
		return
	}

}

func NewWsServerManager() *WsServerManager {
	var upgrader = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}

	return &WsServerManager{
		upgrader: upgrader,
	}

}

func handler(w http.ResponseWriter, r *http.Request) {
	var (
		err  error
		conn *websocket.Conn
	)
	conn, err = upgrader.Upgrade(w, r, nil)
	if err != nil {
		return
	}
	defer conn.Close()

	for {
		messageType, p, err := conn.ReadMessage()
		if err != nil {
			return
		}
		if err := conn.WriteMessage(messageType, p); err != nil {
			return
		}
	}
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}
